********************************************************************************
* Datamanagement: Workplace networks and political selection *******************
********************************************************************************

clear all
clear matrix
set more off, perm
cd "E:/ProjData/Workplace, elite/"
capture log close
log using "C:/Userdata/Shared/Logs/Workplace, elite/datamanagement.text", replace text									
do "C:/Userdata/Shared/Dofiles/DoData/Workplace, elite/programs.do"		


********************************************************************************
* Define the universe of individuals: Birth data 
********************************************************************************

use "Fodelseuppg.dta", replace
gen fodar = substr(FodArMan,1,4) 
destring fodar, replace
drop if fodar > 2000															/*Politician data up until 2018. 18 years before that */	
drop if fodar < 1934 															/*Work place AND occupation data data from 2001. 67 years before that*/	
drop FodArMan 

* Defining variables from core data *
gen immig = .																	// Born in Sweden or abroad
replace immig = 1 if Fodelsevarldsdel != "Sverige"
replace immig = 0 if Fodelsevarldsdel == "Sverige" 
drop Fodelsevarldsdel 

gen gender = .
replace gender = 0 if Kon == 1													// Gender
replace gender = 1 if Kon == 2
drop Kon


********************************************************************************
* Add place of work data (and some covariates...): LISA 
********************************************************************************

* Adding a panel dimension for the years 1990-2018 to the universe
gen year = 2001
xtset LopNr year
tsappend, add(17)																/*Panel 2001-2018*/ /*DÖD?*/
bysort LopNr (year): carryforward gender immig fodar, replace					/*Carryforwarding existing fixed variables*/

* Merge in place of residence data*
merge 1:1 LopNr year using "Deso_panel.dta"
drop if _merge == 2							
drop _merge


* Merging in place of work data (+ covariates)*
merge 1:1 LopNr year using "arbetstalle_panel.dta" 
drop if _merge == 2							
drop _merge

* Adding covariates for 2018 (for which we lack information from modified LISA)
merge 1:1 LopNr year using "covs_2018.dta", update 
drop if _merge == 2							
drop _merge

* Compensating for missing values for WorkID and occupation code *
replace ssyk = "" if ssyk == "****"
save "tempmode", replace														/*For memory reasons: Keep part of the data and compress*/

keep LopNr year ArbstId ssyk   
compress

foreach k in ArbstId ssyk { 
	bysort LopNr (year): egen mode_`k' = mode(`k'), maxmode						// Mode value. Used below 
}

* Interpolating missing values in the panel *
bysort LopNr (year): replace ArbstId = ArbstId[_n+1] if ArbstId == . & ArbstId[_n+1] != . & _n == 1	/*First row missing in panel*/
bysort LopNr (year): replace ArbstId = ArbstId[_n-1] if ArbstId == . & ArbstId[_n-1] != . & ArbstId[_n+1] != .	/*The donut missing value case*/   
bysort LopNr (year): replace ArbstId = ArbstId[_n-1] if ArbstId == . & ArbstId[_n-1] != . & _N == 1	/*Last row missing in panel*/
replace ArbstId = mode_ArbstId if ArbstId == . & mode_ArbstId != .  					/*Use mode for the rest*/							
	
bysort LopNr (year): replace ssyk = ssyk[_n+1] if ssyk == "" & ssyk[_n+1] != "" & _n == 1	
bysort LopNr (year): replace ssyk = ssyk[_n-1] if ssyk == "" & ssyk[_n-1] != "" & ssyk[_n+1] != ""	  
bysort LopNr (year): replace ssyk = ssyk[_n-1] if ssyk == "" & ssyk[_n-1] != "" & _N == 1	
replace ssyk = mode_ssyk if ssyk == "" & mode_ssyk != "" 	 												


save "tempA", replace						


use "tempmode", clear
drop ArbstId ssyk 
merge 1:1 LopNr year using "tempA.dta", keepusing(ArbstId ssyk mode_ArbstId mode_ssyk) keep(1 3) nogen


sort LopNr year
gen dropvarA = 1 if ArbstId == .												/*Dropping all indiviudals where we do not have information on place of work*/
gen dropvarB = 1 if ssyk == ""

bysort LopNr (dropvarA): carryforward dropvarA, replace							/* This also removes indiviudals that are dead.*/ 
bysort LopNr (dropvarB): carryforward dropvarB, replace

drop if dropvarA == 1															
drop if dropvarB == 1


drop dropvar*
sort LopNr year
compress

save "temp1", replace						


********************************************************************************
* Add politician data
********************************************************************************

use "politicians.dta", replace

drop if year < 1998
replace year = 2001 if year == 1998												/*Because the first year for ssyk codes is 2001. Same mandate period*/
bysort LopNr (year): egen firstyearnom = min(year)
label var firstyearnom "First year nominated"

compress
save "politicians_mod.dta", replace


use "temp1", replace

merge 1:1 LopNr year using "politicians_mod.dta", nogen keep(1 3)					/*Data on whether the indiviudal is a politician a given year*/



*Add data on constestability of seats (Olle's code)
rename KKod Kod
joinby Kod firstyearnom parti list_KO using "contestability.dta", unmatched(master)
bysort Kod firstyearnom parti: egen lowest_rank= max(list_KO)


 
*Define constesability categories using the main prediction model
gen top_al =list_KO==1 &  psd_>.9 &psd_!=.
gen safe_al = psd_>.9 &psd_!=. & list_KO!=1
gen cmod_ab_al = psd_<=.9 & psd_>.7
gen chigh_al = psd_<=.7 & psd_>.3
gen cmod_bel_al = psd_<=.3 & psd_>.1
gen sloss_al = psd_<=.1  & list_KO!=.

rename Kod KKod


*Defining politicians variables 1: General*
foreach k in nominated elected nominated_M nominated_KD nominated_L nominated_C nominated_MP nominated_S nominated_V nominated_SD {
	replace `k' = 0 if `k' == .
}


*Bloc variables*
gen nominated_CR = 0
gen nominated_CL = 0
gen nominated_CRSD = 0

replace nominated_CR = 1 if (nominated_M == 1 | nominated_KD == 1 | nominated_L == 1 | nominated_C == 1) 
replace nominated_CL = 1 if (nominated_MP == 1 | nominated_S == 1 | nominated_V == 1)  
replace nominated_CRSD = 1 if (nominated_M == 1 | nominated_KD == 1 | nominated_L == 1 | nominated_C == 1 | nominated_SD == 1) 



* Creating a variable for mandate period: Restricted by 2001-2018 (when we have workplace data that mathcb elected data)*
gen mandatperiod = .
replace mandatperiod = 1 if year == 2001 
replace mandatperiod = 2 if year == 2002 | year == 2003 | year == 2004 | year == 2005 
replace mandatperiod = 3 if year == 2006 | year == 2007 | year == 2008 | year == 2009 
replace mandatperiod = 4 if year == 2010 | year == 2011 | year == 2012 | year == 2013
replace mandatperiod = 5 if year == 2014 | year == 2015 | year == 2016 | year == 2017
replace mandatperiod = 6 if year == 2018



*Only kepping the first year for each mandat period + 2001
keep if year==2001 | year==2002 | year==2006 | year==2010 | year==2014 | year==2018

save "temp2", replace


********************************************************************************
* Create treatment groups: Politicians in workplace in "t-1"
********************************************************************************

assert ArbstId !=.
assert mandatperiod != .
assert ssyk != ""

tostring ArbstId mandatperiod, replace 

replace ssyk = "" if ssyk == "****"												// Just to make sure. 0 changes. 

gen ssyk_3str = substr(ssyk, 1,3)											
replace ssyk_3str = "" if ssyk_3str == "***"									// Just to make sure. 0 changes
 
gen wpssyk = ArbstId + "_" + ssyk_3str + "_" + mandatperiod

replace wpssyk = "" if (ArbstId == ""  | ssyk_3str == "") 						// Just to make sure. 0 changes. 

* Calculate number of politicians for treatment groups (workplace-occupation-mandate): General*

bysort wpssyk: egen count_wpssyk = count(LopNr)									/* Count the number of individuals workplace-occupation cell */
foreach k in nominated elected nominated_CR nominated_CL nominated_CRSD nominated_M nominated_S nominated_SD {
	bysort wpssyk: egen sum_`k' = sum(`k')										/* Sum the number of politicians in a workplace-occupation cell*/
	
	* Generate treatment shares*
	replace sum_`k' = sum_`k' - `k'												/* Share not including the individual's value for nomination*/
	gen m_wpssyk_`k' = sum_`k' / (count_wpssyk - 1)								/* Share of politicians removing the indiviudal on the row. */
	
	replace m_wpssyk_`k' = 0 if m_wpssyk_`k' == .					 			/* Cells with only one persons are undefined.*/
	rename sum_`k' s_wpssyk_`k'													/* Number of politicians*/
}
drop count_wpssyk

* Calculate number of politicians for workplace-mandate: Robustness analysis
	
gen wp_id = ArbstId + "_" + "_" + mandatperiod

replace wp_id = "" if (ArbstId == ""  | ssyk_3str == "") 						


bysort wp_id: egen count_wp_id = count(LopNr)									
bysort wpssyk: egen sum_nominated = sum(nominated)								
	
replace sum_nominated = sum_nominated - nominated								
gen m_wp_nominated = sum_nominated / (count_wp_id - 1)							
	
replace m_wp_nominated = 0 if m_wp_nominated == .					 			
rename sum_nominated s_wp_nominated													

drop count_wp_id 
	
	
	

save "temp3", replace




********************************************************************************
* Ability: Cognitive ability and grades ****************************************
********************************************************************************


merge m:1 LopNr using "conscription.dta", nogen keep(1 3)
merge m:1 LopNr using "conscription2.dta", nogen keep(1 3)
merge m:1 LopNr using "betyg.dta", nogen keep(1 3)

foreach k in mbet GBP {														
	egen mean_`k' = mean(`k'), by(avgar betygsystem)							
	egen sd_`k' = sd(`k'), by(avgar betygsystem)
	gen z`k' = (`k' - mean_`k') / sd_`k'
}

foreach k in Gkap Psyf Befl {														
	egen mean_`k' = mean(`k'), by(Conscriptionyear)							
	egen sd_`k' = sd(`k'), by(Conscriptionyear)
	gen z`k' = (`k' - mean_`k') / sd_`k'
}

drop mean_* sd_*


gen comp = .
replace comp = zGkap if gender == 0 & zGkap !=.
replace comp = zmbet if zmbet != . & zGkap == . & zGBP == .
replace comp = zGBP if zGBP !=. & zGkap == . & zmbet == .

* Competence for a party *

bysort parti: egen comp_parti = mean(comp) if parti != . 


* Binary competence measure vis a vis competence within the party * 
gen b_com_nom = 0							/*0 for all else*/
replace b_com_nom = 1 if comp > comp_parti & comp_parti !=. & comp != . & nominated != . 
gen b_incom_nom = 0							/*0 for all else*/
replace b_incom_nom = 1 if comp <= comp_parti & comp_parti !=. & comp != . & nominated != . 


*The number of competent and non-competent politicians within a workplace-occupation cell * 
bysort wpssyk: egen count_wpssyk = count(LopNr)										
foreach k in b_com_nom b_incom_nom {
	bysort wpssyk: egen sum_`k' = sum(`k')
	
	* Generate treatment shares*
	replace sum_`k' = sum_`k' - `k'												/*Just as above: Share not including the individual's value for nomination*/
	gen m_wpssyk_`k' = sum_`k' / (count_wpssyk - 1)

	replace m_wpssyk_`k' = 0 if m_wpssyk_`k' == .
	rename sum_`k' s_wpssyk_`k'
}
drop count_wpssyk

* Shortening variable names *
rename m_wpssyk_b_incom_nom  m_wpssyk_incomnom
rename m_wpssyk_b_com_nom  m_wpssyk_comnom
rename s_wpssyk_b_incom_nom  s_wpssyk_incomnom
rename s_wpssyk_b_com_nom  s_wpssyk_comnom

save "tempcomp.dta", replace


********************************************************************************
* Covariates on the workplace-occupation cell level in t
********************************************************************************

destring gender, replace
foreach k in yearseducation zink gender immig zdisp zforled HsDelt zakassa {
	bysort wpssyk: egen m_wpssyk_`k' = mean(`k') 
}

save "temp4", replace

********************************************************************************
* Treatment variables: Politicians in "t-1"
********************************************************************************

*Place of work, occupation and mandate period in t-1*
bysort LopNr (mandatperiod): gen wpssyk_lag = wpssyk[_n-1] 

*Place of work and mandate period in t-1 (robustness)
bysort LopNr (mandatperiod): gen wp_id_lag = wp_id[_n-1] 



*Treatment variables main analysis: Number and share of politicians in the previous mandate period*
foreach k in nominated elected nominated_CR nominated_CL nominated_CRSD incomnom comnom nominated_M nominated_S nominated_SD  {
	bysort LopNr (mandatperiod): gen m_wpssyk_`k'_lag = m_wpssyk_`k'[_n-1] 
	bysort LopNr (mandatperiod): gen s_wpssyk_`k'_lag = s_wpssyk_`k'[_n-1] 
}


* Placebo periods: Share of politicians
bysort LopNr (mandatperiod): gen m_wpssyk_nominated_lag2 = m_wpssyk_nominated[_n-2] 
bysort LopNr (mandatperiod): gen m_wpssyk_nominated_lag3 = m_wpssyk_nominated[_n-3] 
bysort LopNr (mandatperiod): gen m_wpssyk_nominated_lag4 = m_wpssyk_nominated[_n-4] 
bysort LopNr (mandatperiod): gen m_wpssyk_nominated_lag5 = m_wpssyk_nominated[_n-5] 


* Treatment variable only workplaces. Robustness analysis *
bysort LopNr (mandatperiod): gen m_wp_nominated_lag = m_wp_nominated[_n-1] 

* Lagged versions of dependent variable. Needs to be used as restrictions in regressions. 
rename b_incom_nom incomnom 
rename b_com_nom comnom

foreach k in nominated elected nominated_CR nominated_CL nominated_CRSD incomnom comnom nominated_M nominated_S nominated_SD {
	bysort LopNr (mandatperiod): gen `k'_lag = `k'[_n-1] 
	bysort LopNr (mandatperiod): gen `k'_lag2 = `k'[_n-2]  						
	bysort LopNr (mandatperiod): gen `k'_lag3 = `k'[_n-3]  						
	bysort LopNr (mandatperiod): gen `k'_lag4 = `k'[_n-4]  						
	bysort LopNr (mandatperiod): gen `k'_lag5 = `k'[_n-5] 
}

* Lagged version of covariates on the workplace. occupation, mandateperiod level.  
foreach k in yearseducation zink gender immig zdisp zforled HsDelt zakassa {
	bysort LopNr (mandatperiod): gen wpssyk_`k'_lag = m_wpssyk_`k'[_n-1]


}

** Number of collegues per workplace, occupation, mandate period cell **
foreach k in wpssyk_lag wp_id_lag {
	bysort `k': egen N_`k' = count(LopNr)
	replace N_`k' = . if `k' == ""
	replace N_`k' = N_`k' - 1													// Only other collegues. 
}

save "temp5", replace



********************************************************************************
* Add election results and mandates (To be used for the RD-analysis)
********************************************************************************

gen Kod = substr(Deso,1,4)														// OBS. Municipality of residence. 

gen LKod = substr(Kod,1,2)
merge m:1 Kod year using "MandatMuni", nogen keep(1 3)						
merge m:1 LKod year using "MandatLandsting", nogen keep(1 3)						
merge m:1 year using "MandatRiksdagen", nogen keep(1 3)						

gen CRbloc_muni = MandatM + MandatKD + MandatL + MandatC
gen CLbloc_muni = MandatMP + MandatS + MandatV
gen Obloc_muni = MandatSD + MandatÖ  

gen CRbloc_La= MandatLM + MandatLKD + MandatLL + MandatLC
gen CLbloc_La = MandatLMP + MandatLS + MandatLV
gen Obloc_La = MandatLSD + MandatLÖ  

gen CRbloc_Ri= MandatRM + MandatRKD + MandatRL + MandatRC
gen CLbloc_Ri = MandatRMP + MandatRS + MandatRV
gen Obloc_Ri = MandatRSD + MandatRND  

gen dif_muni_CR = .
replace dif_muni_CR = CRbloc_muni - CLbloc_muni - Obloc_muni if Obloc_muni != .
replace dif_muni_CR = CRbloc_muni - CLbloc_muni if Obloc_muni == .

gen dif_muni_CL = .
replace dif_muni_CL = CLbloc_muni - CRbloc_muni - Obloc_muni if Obloc_muni != .
replace dif_muni_CL = CLbloc_muni - CRbloc_muni if Obloc_muni == .

gen dif_La_CR = .
replace dif_La_CR = CRbloc_La - CLbloc_La - Obloc_La if Obloc_La != .
replace dif_La_CR = CRbloc_La - CLbloc_La if Obloc_La == .

gen dif_La_CL = .
replace dif_La_CL = CLbloc_La - CRbloc_La - Obloc_La if Obloc_La != .
replace dif_La_CL = CLbloc_La - CRbloc_La if Obloc_La == .

gen dif_Ri_CR = .
replace dif_Ri_CR = CRbloc_Ri - CLbloc_Ri - Obloc_Ri if Obloc_Ri != .
replace dif_Ri_CR = CRbloc_Ri - CLbloc_Ri if Obloc_Ri == .

gen dif_Ri_CL = .
replace dif_Ri_CL = CLbloc_Ri - CRbloc_Ri - Obloc_Ri if Obloc_Ri != .
replace dif_Ri_CL = CLbloc_Ri - CRbloc_Ri if Obloc_Ri == .

 
*Lagged version of running variable*
bysort LopNr (mandatperiod): gen dif_muni_CL_lag = dif_muni_CL[_n-1]
bysort LopNr (mandatperiod): gen dif_La_CL_lag = dif_La_CL[_n-1]
bysort LopNr (mandatperiod): gen dif_Ri_CL_lag = dif_Ri_CL[_n-1]

bysort LopNr (mandatperiod): gen dif_muni_CR_lag = dif_muni_CR[_n-1]
bysort LopNr (mandatperiod): gen dif_La_CR_lag = dif_La_CR[_n-1]
bysort LopNr (mandatperiod): gen dif_Ri_CR_lag = dif_Ri_CR[_n-1]

bysort LopNr (mandatperiod): gen muni_CR_lag = CRbloc_muni[_n-1]
bysort LopNr (mandatperiod): gen muni_CL_lag = CLbloc_muni[_n-1]
bysort LopNr (mandatperiod): gen muni_O_lag = Obloc_muni[_n-1]

rename Kod Kommunresident


********************************************************************************
* Adding placebo outcomes
********************************************************************************

* Consciption data *
merge m:1 LopNr using "conscription.dta", nogen keep(1 3)

* Grades from elementary school 
merge m:1 LopNr using "AK9.dta", nogen keep(1 3)
save "temp6", replace

********************************************************************************
* Adding population data for the municipality **********************************
********************************************************************************

rename KKod Kod
merge m:1 Kod year using "mandatpop.dta", nogen keep(1 3)
rename Kod Kkod 


********************************************************************************
* Naming, standardizing, labeling etc 
********************************************************************************

*Naming outcome variables in "t"*
rename nominated i_nominated
rename nominated_lag i_nominated_lag
rename nominated_lag2 i_nominated_lag2
rename nominated_lag3 i_nominated_lag3
rename nominated_lag4 i_nominated_lag4
rename nominated_lag5 i_nominated_lag5

rename nominated_CR i_nominated_CR
rename nominated_CL i_nominated_CL
rename nominated_CRSD i_nominated_CRSD
rename nominated_CR_lag i_nominated_CR_lag
rename nominated_CL_lag i_nominated_CL_lag
rename nominated_CRSD_lag i_nominated_CRSD_lag

rename nominated_M i_nominated_M
rename nominated_S i_nominated_S
rename nominated_SD i_nominated_SD
rename nominated_M_lag i_nominated_M_lag
rename nominated_S_lag i_nominated_S_lag
rename nominated_SD_lag i_nominated_SD_lag


rename incomnom i_nominated_incom 
rename comnom i_nominated_com 

rename elected i_elected

/*
* Standardizing *
foreach k in m_wpssyk_nominated_lag m_wpssyk_incomnom_lag m_wpssyk_comnom_lag m_wpssyk_elected_lag m_wpssyk_nominated_CR_lag m_wpssyk_nominated_CL_lag m_wpssyk_nominated_CRSD_lag { 
	egen z_`k' = std(`k'), mean(0) std(1)
}
*/

* Scale treatment variables: Number of politicians per 10 workers
gen maintreatvar = m_wpssyk_nominated_lag * 10
gen crtreatvar = m_wpssyk_nominated_CR_lag * 10
gen cltreatvar = m_wpssyk_nominated_CL_lag * 10
gen hightreatvar = m_wpssyk_comnom_lag * 10
gen lowtreatvar = m_wpssyk_incomnom_lag * 10 
gen electvar = m_wpssyk_elected_lag * 10
gen crsdtreatvar = m_wpssyk_nominated_CRSD_lag * 10
gen wptreatvar = m_wp_nominated_lag * 10

gen sdtreatvar = m_wpssyk_nominated_SD_lag * 10
gen Streatvar = m_wpssyk_nominated_S_lag * 10
gen Mtreatvar = m_wpssyk_nominated_M_lag * 10

gen treatlag2 = m_wpssyk_nominated_lag2 * 10
gen treatlag3 = m_wpssyk_nominated_lag3 * 10
gen treatlag4 = m_wpssyk_nominated_lag4 * 10
gen treatlag5 = m_wpssyk_nominated_lag5 * 10



* Labels
label var i_nominated "Nom." 
label var i_elected "Elec."
label var i_nominated_CR "Nom. CR" 
label var i_nominated_CRSD "Nom. CR + SD" 
label var i_nominated_CL "Nom. CL" 
label var i_elected "Elect."
label var i_nominated_incom "Nom low."
label var i_nominated_com "Nom high"

label var i_nominated_M "Nom. M" 
label var i_nominated_SD "Nom. SD" 
label var i_nominated_S "Nom. S" 


label var maintreatvar "Politicians per 10 colleagues in t-1"
label var crtreatvar "CR per 10 colleagues in t-1" 
label var cltreatvar "CL per 10 colleagues in t-1" 
label var hightreatvar "High skill per 10 colleagues in t-1" 
label var lowtreatvar "Low skill per 10 colleagues in t-1" 
label var electvar "Elected per 10 colleagues in t-1" 
label var crsdtreatvar "CR + SD per 10 colleagues in t-1"
label var wptreatvar "Politicians per 10 colleagues in t-1" 
label var Streatvar "S per 10 colleagues in t-1" 
label var Mtreatvar "M per 10 colleagues in t-1" 
label var sdtreatvar "SD per 10 colleagues in t-1" 

*label var z_m_wpssyk_nominated_lag "Share politicians in t-1"
*label var z_m_wpssyk_nominated_CR_lag "Share CR politicians in t-1"
*label var z_m_wpssyk_nominated_CL_lag "Share CL politicians in t-1"
*label var z_m_wpssyk_incomnom_lag "Share low skill t-1"
*label var z_m_wpssyk_comnom_lag "Share high skill t-1"
*label var z_m_wpssyk_nominated_CRSD_lag "Share CR + SD politicians in t-1"
*label var z_m_wpssyk_elected_lag "Share elected pol. in t-1"
label var s_wpssyk_nominated_lag "N. politicians in t-1"
label var m_wpssyk_nominated_lag "Share politicians in t-1" 
label var N_wpssyk_lag "N workers in workplace-occupation-mandate cell" 
label var treatlag2 "Politicians per 10 colleagues in t-2"
label var treatlag3 "Politicians per 10 colleagues in t-3"
label var treatlag4 "Politicians per 10 colleagues in t-4"
label var treatlag5 "Politicians per 10 colleagues in t-5"


*remake string variables for i.use
sort ArbstId 
egen wp  = group(ArbstId)
sort ssyk_3str 
egen occupation = group(ssyk_3str)
destring mandatperiod Kkod, replace

* Generate interaction terms for heterogeneity analysis *
gen crd = 0
replace crd = 1 if (muni_CR_lag >= muni_CL_lag) & (muni_CR_lag >= muni_O_lag) & muni_CR_lag != . 
replace crd = . if (muni_CR_lag == . | muni_CL_lag == . | muni_O_lag == .)  

gen cld = 0 
replace cld = 1 if (muni_CL_lag >= muni_CR_lag) & (muni_CL_lag >= muni_O_lag) & muni_CL_lag != .
replace cld = . if (muni_CR_lag == . | muni_CL_lag == . | muni_O_lag == .)  


gen intercr = crd * crtreatvar 
gen intercl = cld * cltreatvar   

label var crd "CR plur."
label var cld "CL plur."
label var intercr "CR per 10 colleagues. in t-1 * CR plur."
label var intercl "CL per 10 colleagues. in t-1 * CL plur."
 

* Keeping the relevant variables for dataanalysis and robustness *
keep LopNr year mandatperiod wp Sni occupation yearseducation zink wpssyk_zink_lag wpssyk_yearseducation_lag wpssyk_gender_lag wpssyk_immig_lag i_nominated i_nominated_lag i_nominated_lag2 i_nominated_lag3 i_nominated_lag4 i_nominated_lag5 Kkod i_nominated_CR i_nominated_CL i_nominated_S i_nominated_SD i_nominated_M dif_muni_CR_lag dif_muni_CL_lag s_wpssyk_nominated_CR_lag s_wpssyk_nominated_CL_lag s_wpssyk_nominated_SD_lag s_wpssyk_nominated_S_lag s_wpssyk_nominated_M_lag muni_pop zink zdisp zforled zakassa i_elected top_al safe_al cmod_ab_al chigh_al cmod_bel_al sloss_al ssyk_3str N_wpssyk_lag i_nominated_com i_nominated_incom i_nominated_CRSD fodar immig gender comp LoneInk Akassa DispInk ForLed muni_CR_lag muni_CL_lag muni_O_lag m_wpssyk_nominated_lag s_wpssyk_nominated s_wpssyk_nominated_lag HsDelt crd cld intercr intercl maintreatvar crtreatvar cltreatvar Mtreatvar Streatvar sdtreatvar hightreatvar lowtreatvar electvar crsdtreatvar wptreatvar N_wp_id_lag treatlag2 treatlag3 treatlag4 treatlag5 
order LopNr year mandatperiod i_nominated i_elected maintreatvar crtreatvar cltreatvar hightreatvar lowtreatvar m_wpssyk_nominated_lag s_wpssyk_nominated_lag s_wpssyk_nominated i_nominated_lag* wp occupation 
compress
sort LopNr year

**********************
save "finaldata.dta", replace
capture log close
clear all



****

